/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import com.bourre.data.collections.Map;

import fever.app.browser.analytic.strategy.AbstractAnalyticStrategy;
import fever.commands.JSCommand;
import fever.commands.JSMethodName;

/**
 * Track application / site internal navigation throw BlueStreak engine.
 * 
 * @author Romain Ecarnot
 * 
 * //TODO using internal js engine to call method.
 */
class fever.app.browser.analytic.strategy.BlueStreak 
	extends AbstractAnalyticStrategy
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private var _baseURL : String;
	private var _mAssociation : Map;
	private var _jsMethod : JSCommand;	
	
	//-------------------------------------------------------------------------
	// Public Properties
	//-------------------------------------------------------------------------
	
	/** Javascript method name to call. */
	public static var trackMETHOD : JSMethodName = new JSMethodName( 'trackBlueStreak' );
	
	/**
	 * Base url for <strong>BlueStreak</strong> tracking.
	 */
	public function get baseURL() : String { return _baseURL; }
	public function set baseURL ( p : String ) : Void { _baseURL = p;	}
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 * 
	 * 
	 */
	public function BlueStreak( url : String )
	{
		super();
		
		_mAssociation = new Map();
		_baseURL = url;
		_jsMethod = new JSCommand( 
			trackMETHOD,
			"( url ){ bskImg = new Image(); bskImg.src = url; return; }"
		);
	}
	
	/**
	 * Associates passed-in {@code link} with {@code bsID}  
	 * <strong>BlueStreak</strong> tracking ID.
	 * 
	 * @param link Real link to track
	 * @param bsID Associated <strong>BlueStreak</strong> tracking ID
	 */
	public function register( link : String, bsID : String ) : Void
	{
		_mAssociation.put( _buildCorrectLink( link ), bsID );
	}
	
	/**
	 * Removes passed-in {@code link} from <strong>BlueStreak</strong> 
	 * tracking.
	 * 
	 * @param link Real link to remove
	 */
	public function unregister( link : String ) : Void
	{
		_mAssociation.remove( _buildCorrectLink( link ) );
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	private function _track( link : String ) : Void
	{
		if( _mAssociation.containsKey( link ) && _baseURL )
		{	
			_jsMethod.call( _mAssociation.get( link ) );
		}
	}
}